<template>
  <div class="login_container">
    <div class="login_box">
      <h1 style=" text-align: center;">管理系统</h1>
      <!-- ref,reference,获取元素  -->
      <!-- v-model绑定元素 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        label-width="80px"
        class="login_form"
        :rules="loginRules"
      >
        <!-- 数据绑定v-model -->
        <!-- 用户名 -->
        <el-form-item
          label="用户名"
          style="padding: 0 85px 0 0"
          prop="username"
        >
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item
          label="密码"
          style="padding: 0 85px 0 0"
          prop="password"
          ref="loginFormRef"
        >
          <el-input v-model="loginForm.password" type="password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <div class="bts">
          <el-button type="primary" style=" margin-left: 220px;" @click="login"
            >登录</el-button
          >
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "Login",
  data() {
    return {
      // 绑定数据
      loginForm: {
        username: "admin",
        password: "123456"
      },
      //   表单校验
      loginRules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //   重置，恢复默认值
    resetLoginForm() {
      // this.$refs.loginFormRef拿到表单对象
      this.$refs.loginFormRef.resetFields();
    },
    //登录
    login() {
      //   console.log("submit!");
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return;
        //请求接口，封装数据
        const { data: res } = await this.$http.post("login", this.loginForm);
        if (res.meta.status !== 200) {
          //弹窗提示
          return this.$message.error("登录失败");
        } else {
          this.$message.success("登录成功");
          // 登录后的行为：
          // 1，将登录成功后的token保存到客户端的sessionStorage中
          //     1.1 项目中出现了登录之外的其他API接口，必须登录后访问
          //     1.2 token只在当前网站打开期间生效，所以token必须保存在sessionStorage
          window.sessionStorage.setItem("token", res.data.token);
          // 2，通过编程式导航跳转到后台主页，路由地址/home
          this.$router.push("/home");
        }
      });
    }
  }
};
</script>
<style scoped>
.login_container {
  height: 100%;
  background-color: #5dbe8a !important;
}
.login_box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  /* background-color: #5dbe8a !important; */
  border-radius: 3px;
}
.login_form {
  box-sizing: border-box;
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 10px;
  margin: 25px;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
